<template>
  <div>
    <el-dialog title="收货地址" :visible="showDialog" @close="closeFn">
      <el-form ref="form" :model="form">
        <el-form-item label="经纪" label-width="80px">
          <el-select v-model="form.agent" placeholder="请选择经纪">
            <el-option v-for="item in agentsList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="分类" label-width="80px">
          <el-select v-model="form.category" placeholder="请选择分类">
            <el-option v-for="item in fenleiList" :key="item.id" :label="item.title" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" label-width="80px">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="标签" label-width="80px">
          <el-tag
            v-for="tag in form.tags"
            :key="tag"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)"
          >
            {{ tag }}
          </el-tag>
          <el-input
            v-if="inputVisible"
            ref="saveTagInput"
            v-model="inputValue"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          />
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
        </el-form-item>
        <el-form-item label="地址" label-width="80px">
          <el-cascader
            v-model="selectedOptions"
            size="large"
            :options="options"
            style="width:40%;display:inline-block"
            @change="handleChange"
          />
          <el-input v-model="form.address" :valde="form.address" disabled style="display:inline-block;width:50%" />
        </el-form-item>
        <el-form-item label="价格" label-width="80px">
          <el-input v-model="form.price" />
        </el-form-item>
        <el-form-item label="描述" label-width="80px">
          <el-input v-model="form.desc" type="textarea" :rows="2" />
        </el-form-item>
        <el-form-item label="内容" label-width="80px">
          <vue-editor v-model="form.content" />
        </el-form-item>
        <el-form-item label="介绍图片" label-width="80px">
          <ImgWpy ref="imgUploadWPY" />
        </el-form-item>
        <el-form-item label="封面" label-width="80px">
          <ImageUpload ref="imgUpload" :staff-photo.sync="form.image"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="confirmFn(form.id)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { regionData, CodeToText, TextToCode } from 'element-china-area-data'
// import { VueEditor } from 'vue2-editor'
import { addFangchan, oneFangchan, editFangchan, delFangchan } from '@/api/fangchan'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    agentsList: {
      type: Array,
      default() {
        return []
      }
    },
    fenleiList: {
      type: Array,
      default() {
        return []
      }
    },
    res: {
      type: Array
    }
  },
  data() {
    return {
      inputVisible: false,
      inputValue: '',
      form: {
        id: '',
        title: '', // 标题
        desc: '', // 描述
        image: '', // 图片
        agent: '', // 属于哪个经纪的 id 值
        content: '', // 内容
        address: '', // 地址
        price: '', // 价格
        photos: [], // 图片, url 组成数组
        tags: [], // 标签, 字符串组成数组
        category: '' // 属于哪个分类 id 值
      },
      selectedOptions: [],
      options: regionData,
      china: {
        provinces: '',
        city: '',
        area: ''
      }
    }
  },
  methods: {
    closeFn() {
      this.$emit('update:showDialog', false)
      this.$refs.imgUploadWPY.fileList = []
      // this.$refs.imgUpload.fileList = []
      this.$refs.imgUpload.removeFn()
      this.form = {
        id: '',
        title: '', // 标题
        desc: '', // 描述
        image: '', // 图片
        agent: '', // 属于哪个经纪的 id 值
        content: '', // 内容
        address: '', // 地址
        price: '', // 价格
        photos: [], // 图片, url 组成数组
        tags: [], // 标签, 字符串组成数组
        category: '' // 属于哪个分类 id 值
      }
      this.selectedOptions = []
      this.china = {
        provinces: '',
        city: '',
        area: ''
      }
    },
    // 删除标签
    handleClose(tag) {
      this.form.tags.splice(this.form.tags.indexOf(tag), 1)
    },
    // 标签变输入框
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 输入框变标签回调
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.form.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    handleChange() {
      this.china.provinces = CodeToText[this.selectedOptions[0]]
      this.china.city = CodeToText[this.selectedOptions[1]]
      this.china.area = CodeToText[this.selectedOptions[2]]
      this.form.address = this.china.provinces + ',' + this.china.city + ',' + this.china.area
    },
    async confirmFn(id) {
      if (id) {
        // this.form.photos = this.$refs.imgUploadWPY.fileList
        this.form.photos = this.$refs.imgUploadWPY.fileList.map(item => item.url)
        await editFangchan(this.form)
      } else {
        this.form.photos = this.$refs.imgUploadWPY.fileList.map(item => item.url)
        await addFangchan(this.form)
      }
      this.$message.success('添加成功')
      this.closeFn()
      this.$emit('ok')
      this.$emit('total')
    },
    async editFn(id) {
      this.form = await oneFangchan(id)
      console.log(this.form.photos)
      const ok = this.form.photos.map(item => item = { url: item })
      this.$refs.imgUploadWPY.fileList = ok

      this.$refs.imgUpload.fileList.push({
        url: this.form.image
      })

      console.log(this.form.image);
      const address = this.form.address.split(',')
      this.china.provinces = address[0]
      this.china.city = address[1]
      this.china.area = address[2]
      this.selectedOptions = []
      this.selectedOptions.push(TextToCode[this.china.provinces].code, TextToCode[this.china.provinces][this.china.city].code, TextToCode[this.china.provinces][this.china.city][this.china.area].code)
    },
    async delFn(row) {
      this.$confirm(`是否删除${row.title}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delFangchan(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.closeFn()
        this.$emit('ok')
        this.$emit('total')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
